home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 11 / CU Amiga Magazine's Super CD-ROM 11 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-06].iso / s / mailmerge.pprx < prev    next >
Text File  |  1993-02-15  |  7KB  |  305 lines

  1. /*
  2. @BMailMerge  @P@ICopyright Gold Disk Inc., Jan, 1993
  3.  
  4. This genie will load a tab or comma delimited merge file and print multiple copies of the current document. It will substitute the merge string in appropriate places.
  5.  
  6. In order to delimit a field as a merge field, you must surround the merge
  7. text with double guilme's. Example:««Name»»
  8.    Open guilme: alt-9
  9.    Closing guilme: alt-0
  10. */
  11.  
  12. parse arg filename
  13. if ~show(l, "rexxsupport.library") then
  14.     if ~addlib("rexxsupport.library",0,-30) then
  15.         call exit_msg("Please install the rexxsupport.library in your libs: directory before running this Genie")
  16.  
  17. signal on error
  18. signal on syntax
  19. signal on break_c
  20. signal on break_d
  21. signal on break_e
  22. signal on halt
  23. signal on ioerr
  24. address command
  25. call SafeEndEdit.rexx()
  26. call ppm_SetBatchMode(1)
  27. call ppm_AutoUpdate(0)
  28. cr = '0a'x
  29.  
  30. npages  = ppm_NumPages()
  31. if npages = 0 then exit_msg("You must have a merge document prepared to run this Genie.")
  32.  
  33. if filename = '' then
  34.     filename = ppm_GetFileName("Select Merge File","", "")
  35.  
  36. if filename = '' then exit_msg()
  37.  
  38. if ~open(file, filename, "r") then
  39.     call exit_msg("Unable to open "file)
  40.  
  41. line = readln(file)
  42.  
  43. delimiter = ''
  44.  
  45. if pos('",', line) ~= 0 then
  46. do
  47.     delimiter   = '",'
  48.     qt          = '"'
  49.     rparseline  = "parse var line matrix.0.fields '"delimiter"' line"
  50.     parseline   = "parse var line matrix.records.field '"delimiter"' line"
  51.     rstripline  = "matrix.0.fields = upper(strip(matrix.0.fields, b, '"qt"'))"
  52.     stripline   = "matrix.records.field = strip(matrix.records.field,b,'"qt"')"
  53. end
  54. else if pos('09'x, line) ~= 0 then
  55. do
  56.     rparseline  = "parse var line matrix.0.fields '09'x line"
  57.     parseline   = "parse var line matrix.records.field '09'x line"
  58.     rstripline  = "matrix.0.fields = upper(strip(matrix.0.fields))"
  59.     stripline   = "matrix.records.fields = strip(matrix.records.field)"
  60. end
  61. else if length(line) < 255 then
  62. do
  63.     rparseline  = "matrix.0.fields = upper(strip(line));line = ''"
  64.     parseline   = "matrix.records.field = strip(line);line = ''"
  65.     stripline   = "/**/"
  66.     rstripline  = "/**/"
  67. end
  68. else
  69.     exit_msg("File must be either comma delimited or tab delimited")
  70.  
  71.  
  72. fields = 0
  73. records  = 0
  74. do while line ~ = ''
  75.  
  76.     fields = fields + 1
  77.     interpret rparseline
  78.     interpret rstripline
  79. end
  80.  
  81.  
  82. do while ~eof(file)
  83.  
  84.     line = readln(file)
  85.  
  86.     if line = '' then iterate
  87.  
  88.     records = records + 1
  89.  
  90.     call ppm_ShowStatus("Importing record "records)
  91.  
  92.     do field = 1 to fields
  93.  
  94.         interpret parseline
  95.         interpret stripline
  96.         if line = '' then leave
  97.     end
  98. end
  99.  
  100. call close(file)
  101.  
  102. call ppm_ShowStatus("Preparing document for mail merge..")
  103. boxes   = 1
  104.  
  105. call analyzeboxes()
  106.  
  107. reccount = 1
  108. boxnum  = 1
  109. btext   = 1
  110. ctext   = 2
  111. field   = 1
  112.  
  113. if doctext.0 < 1 then exit_msg("No merge codes found")
  114.  
  115. do while reccount <= records
  116.  
  117.     do boxes = 1 to doctext.0
  118.  
  119.         box = doctext.boxes
  120.         tlen    = 0
  121.  
  122.         boxtext = doctext.boxes.btext
  123.  
  124.         do mergpos = 1 to doctext.boxes.btext.0
  125.  
  126.             field   = doctext.boxes.btext.mergpos.1
  127.             if field = 'NEXT' then
  128.             do
  129.                 reccount = reccount + 1
  130.                 ftext   = ''
  131.             end
  132.             else if left(field, 1) = '=' then
  133.             do
  134.                 string = ''
  135.                 address command
  136.                 interpret "string "field
  137.                 address
  138.  
  139.                 ftext = string
  140.             end
  141.             else
  142.                 ftext   = matrix.reccount.field
  143.  
  144.             fpos    = doctext.boxes.btext.mergpos
  145.             boxtext = insert(ftext, boxtext, fpos - 1 + tlen)
  146.             tlen    = tlen + length(ftext)
  147.  
  148.         end
  149.  
  150.         call ppm_ShowStatus("Merging record "reccount" of "records)
  151.                 call ppm_DeleteContents(box)
  152.                 call ppm_TextIntoBox(box, boxtext)
  153.  
  154.     end
  155.  
  156.     reccount = reccount + 1
  157.  
  158.     lastpage = ppm_DocLastPage()
  159.  
  160.     do page = 1 to npages
  161.  
  162.         call ppm_CopyPage(page, lastpage + 1, 1)
  163.         lastpage = lastpage + 1
  164.  
  165.     end
  166.  
  167.  
  168. end
  169.  
  170. do i = 1 to doctext.0
  171.  
  172.     box = doctext.i
  173.     text    = doctext.i.2
  174.  
  175.         call ppm_DeleteContents(box)
  176.         call ppm_TextIntoBox(box, text)
  177.         call ppm_SetBoxUserData(box, "")
  178.  
  179. end
  180.  
  181. exit_msg("Done")
  182.  
  183.  
  184. exit_msg: procedure 
  185. do
  186.     parse arg message
  187.  
  188.     if message ~= '' then call ppm_Inform(1,message,)
  189.     call ppm_SetBatchMode(0)
  190.     call ppm_ClearStatus()
  191.     call ppm_AutoUpdate(1)
  192.     exit
  193. end
  194.  
  195.  
  196. analyzeboxes: procedure expose doctext. matrix. fields
  197. do
  198.         randval = (randu() * time(s)) % 1
  199.     boxnum  = 1
  200.     rpos    = 1
  201.     btext   = 1
  202.     ctext   = 2
  203.     field   = 1
  204.  
  205.     box = ppm_DocFirstBox()
  206.  
  207.     do while box ~= 0
  208.  
  209.         if upper(word(ppm_GetBoxInfo(box), 1)) ~= TEXT | ppm_GetBoxUserData(box) = randval then
  210.         do
  211.             box = ppm_DocNextBox(box)
  212.             iterate
  213.         end
  214.  
  215.                 oldbox = box
  216.                 box = ppm_ArtFirstBox(box)
  217.                 boxtext = ppm_GetArticleText(box, 1)
  218.  
  219.         copy    = boxtext
  220.         fpos    = pos('««', boxtext)
  221.  
  222.         if fpos ~= 0 then
  223.         do
  224.             doctext.boxnum  = box
  225.             rpos    = 1
  226.  
  227.             do while fpos ~= 0
  228.                                 found = 0
  229.                 epos    = pos('»»', boxtext, fpos)
  230.                 if epos = 0 then leave
  231.                 fieldlen    = epos - fpos + 2
  232.                 temptext   = strip(substr(boxtext, fpos + 2, fieldlen - 4))
  233.                 fieldtext   = upper(temptext)
  234.                 if fieldtext = 'NEXT'  then
  235.                                 do
  236.                                         found    = 1
  237.                     fieldnum = 'NEXT'
  238.                                 end
  239.                 else if left(fieldtext, 1) = '=' then
  240.                                 do
  241.                                         found    = 1
  242.                     fieldnum = temptext
  243.                                 end
  244.                 else
  245.                 do fieldnum = 1 to fields
  246.  
  247.                     if matrix.0.fieldnum = fieldtext then
  248.                                         do
  249.                                                 found = 1
  250.                                                 leave fieldnum
  251.                                         end
  252.                 end
  253.  
  254.                                 if found then
  255.                                 do
  256.                         boxtext = delstr(boxtext, fpos, fieldlen)
  257.                         doctext.boxnum.btext.rpos = fpos
  258.                         doctext.boxnum.btext.rpos.1 = fieldnum
  259.                         rpos = rpos + 1
  260.                                 end
  261.  
  262.                                 fpos = pos('««', boxtext, fpos + 1)
  263.             end
  264.  
  265.             doctext.boxnum.btext = boxtext
  266.             doctext.boxnum.ctext = copy
  267.  
  268.             doctext.boxnum.btext.0 = rpos - 1
  269.             doctext.0   = boxnum
  270.             boxnum  = boxnum + 1
  271.  
  272.         end
  273.  
  274.                 do while box ~= 0
  275.  
  276.                         call SetBoxUserData(box, randval)
  277.                         box = ppm_ArtNextBox(box)
  278.  
  279.                 end
  280.  
  281.         box = ppm_DocNextBox(oldbox)
  282.  
  283.     end
  284.  
  285.     return
  286.  
  287. end
  288.  
  289.  
  290.  
  291. error:
  292. syntax:
  293. break_c:
  294. break_d:
  295. break_e:
  296. break_f:
  297. halt:
  298. ioerr:
  299. novalue:
  300. do
  301.     call ppm_Inform(1,"An Arexx error has occured interpeting Arexx string: "errortext(rc)" Line "SIGL,)
  302.     return
  303. end
  304.  
  305.